// pages/catalog/catalog.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    catalogList:[],
    bookAbbr:"",
    bookId:1,
    options:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCatalog(options)
    this.setData({options:options})
  },
  async getCatalog (options) {
    let app = getApp()
    this.setData({bookAbbr:options.bookAbbr,bookId:options.bookId})
    wx.request({
      url: app.globalData.url + '/book/listcatalog',
      data: {
        bookId: options.bookId,
        id:app.globalData.userInfo.openid,
      },
      success: result => {
        let catalogList = result.data.data
        for(let i = 0; i < catalogList.length; ++i){
          catalogList[i].percent = catalogList[i].practiceSize / catalogList[i].chapterSize * 100
        }
        this.setData({catalogList:catalogList})
      }
    })
  },

  goToPractice:function(event){
    let chapterId = event.currentTarget.dataset.chapterId
    let chapterAbbr = event.currentTarget.dataset.chapterAbbr
    let percent = event.currentTarget.dataset.percent

    if(percent == 100){
      wx.showToast({
        title: '该章节题目全部做完了哦',
        icon: 'none',
        duration: 1000
      })      
    }else{
      wx.navigateTo({
        url: '/pages/question/question?chapterId='+chapterId + "&chapterAbbr=" + chapterAbbr+"&bookAbbr=" + this.data.bookAbbr+"&bookId="+this.data.bookId,
      })
    }
    
    
  },


  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },

  onUnload: function () {

  },
  async onPullDownRefresh () {
    console.log("option:", this.data.options)
    await this.getCatalog(this.data.options)
    wx.stopPullDownRefresh()
  },


  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})